/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gestores;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;

/**
 *
 * @author radxt
 */
public class XMLModificar {

    File fXmlFile;
    DocumentBuilderFactory dbFactory;
    DocumentBuilder dBuilder;
    Document doc;

    public XMLModificar(String file) {
        try {
            this.fXmlFile = new File(file);
            this.dbFactory = DocumentBuilderFactory.newInstance();
            this.dBuilder = dbFactory.newDocumentBuilder();
            this.doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ModificarPass(String code) {
        try {
            //            Node init = doc.getFirstChild();
            //            Node user = doc.getElementsByTagName("").item(0);
            //            int i = 0;
            //            NodeList list = user.getChildNodes();
            ////            for(i = 0; i < list.getLength(); i++){
            //                Node node = list.item(i);
            //                System.out.println(user;
            //           // }
            NodeList list = doc.getElementsByTagName("Usuarios");
            int i = 0;
            while (!list.item(0).getChildNodes().item(i).getAttributes().item(1).getNodeValue().equals(code)) {
                i++;
            }

            list.item(0).getChildNodes().item(i).getAttributes().item(4).setNodeValue(Integer.toString("asdf".hashCode()));
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(this.fXmlFile);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void ModificarLogin(String code, String login) {
        try {
            //            Node init = doc.getFirstChild();
            //            Node user = doc.getElementsByTagName("").item(0);
            //            int i = 0;
            //            NodeList list = user.getChildNodes();
            ////            for(i = 0; i < list.getLength(); i++){
            //                Node node = list.item(i);
            //                System.out.println(user;
            //           // }
            NodeList list = doc.getElementsByTagName("Usuarios");
            int i = 0;
            while (!list.item(0).getChildNodes().item(i).getAttributes().item(1).getNodeValue().equals(code)) {
                i++;
            }

            list.item(0).getChildNodes().item(i).getAttributes().item(2).setNodeValue(login);
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(this.fXmlFile);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void ModificarNombre(String code, String nombre) {
        try {
            //            Node init = doc.getFirstChild();
            //            Node user = doc.getElementsByTagName("").item(0);
            //            int i = 0;
            //            NodeList list = user.getChildNodes();
            ////            for(i = 0; i < list.getLength(); i++){
            //                Node node = list.item(i);
            //                System.out.println(user;
            //           // }
            NodeList list = doc.getElementsByTagName("Usuarios");
            int i = 0;
            while (!list.item(0).getChildNodes().item(i).getAttributes().item(1).getNodeValue().equals(code)) {
                i++;
            }

            list.item(0).getChildNodes().item(i).getAttributes().item(3).setNodeValue(nombre);
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(this.fXmlFile);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
            public void ModificarID(String code, String newCode){
            try {
                //            Node init = doc.getFirstChild();
                //            Node user = doc.getElementsByTagName("").item(0);
                //            int i = 0;
                //            NodeList list = user.getChildNodes();
                ////            for(i = 0; i < list.getLength(); i++){
                //                Node node = list.item(i);
                //                System.out.println(user;
                //           // }
                            NodeList list = doc.getElementsByTagName("Usuarios");
                            int i = 0;
                            while(!list.item(0).getChildNodes().item(i).getAttributes().item(1).getNodeValue().equals(code)){
                                i++;
                            }
                            
                            list.item(0).getChildNodes().item(i).getAttributes().item(1).setNodeValue(newCode);
                                            // write the content into xml file
                                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                                Transformer transformer = transformerFactory.newTransformer();
                                DOMSource source = new DOMSource(doc);
                                StreamResult result = new StreamResult(this.fXmlFile);
                                transformer.transform(source, result);
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerException ex) {
                Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
            }
}
            
                    public void ModificarPermiso(String code, int permiso){
            try {
                //            Node init = doc.getFirstChild();
                //            Node user = doc.getElementsByTagName("").item(0);
                //            int i = 0;
                //            NodeList list = user.getChildNodes();
                ////            for(i = 0; i < list.getLength(); i++){
                //                Node node = list.item(i);
                //                System.out.println(user;
                //           // }
                            NodeList list = doc.getElementsByTagName("Usuarios");
                            int i = 0;
                            while(!list.item(0).getChildNodes().item(i).getAttributes().item(1).getNodeValue().equals(code)){
                                i++;
                            }
                            
                            list.item(0).getChildNodes().item(i).getAttributes().item(0).setNodeValue(Integer.toString(permiso));
                                            // write the content into xml file
                                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                                Transformer transformer = transformerFactory.newTransformer();
                                DOMSource source = new DOMSource(doc);
                                StreamResult result = new StreamResult(this.fXmlFile);
                                transformer.transform(source, result);
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TransformerException ex) {
                Logger.getLogger(XMLModificar.class.getName()).log(Level.SEVERE, null, ex);
            }
                    }
}